package com.zsrt.javaweb;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class PasswordFilter implements Filter {

	private FilterConfig fc;
	
	
	public void init(FilterConfig config) throws ServletException {
		this.fc=config;
	}

	public void destroy() {
	}

	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		
		String password = request.getParameter("password");
		
		//拿到当前web应用的初始化参数
		String webPassword = fc.getServletContext().getInitParameter("password");
		
		if(!password.equals(webPassword)){
			
			//放置错误提示消息
			request.setAttribute("message", "密码错误...");
			
			//如果password不等于"123456"就将请求转发到login.jsp
			request.getRequestDispatcher("/login.jsp").forward(request, response);
			
			//表示不执行后面的方法 不交给下一个过滤器处理
			return;
		}
		chain.doFilter(request, response);
	}

}
